/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VueOuvrageGUI.java
 *
 * Created on 12 déc. 2008, 11:54:28
 */

package biblio;

import java.util.*;
/**
 *
 * @author ksz
 */
public class VueOuvrageGUI extends javax.swing.JDialog implements Observer {

    private Ouvrage ouvrage;
    private java.awt.Frame parent_frame;
    
    /** Creates new form VueOuvrageGUI */
    public VueOuvrageGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        parent_frame = parent;
        initComponents();
        this.setLocationRelativeTo(this.getParent());
    }

    public void menuPrincipal(Ouvrage ouv) {
        ouvrage = ouv;
        ouvrage.addObserver(this);
        this.afficherTout();
        this.setVisible(true);
    }

     //********************************
     public void elimineObserveur() {
        ouvrage.deleteObserver(this);
     } // Fin elimineObserveur

    private void afficherTout()  {
        isbn_textfield.setText(ouvrage.isbn());
        titre_textfield.setText(ouvrage.titre());
        this.afficherListeAuteurs();
        editeur_textfield.setText(ouvrage.editeur());
        this.afficherListeMotsCles();
        java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("dd/MM/yyyy");
        java.util.Date dateDate = ouvrage.dateEdition().getTime();
        String dateString = dateFormat.format(dateDate);
        date_textfield.setText(dateString);

        nbexemp_textfield.setText(Integer.toString(ouvrage.nbExemplaires()));
        
        this.afficherListeExemplaires();

    }

    private void afficherListeAuteurs() {
        Set<Auteur> auteurs  = ouvrage.tesAuteurs();
        javax.swing.table.DefaultTableModel dtmAuteurs = new javax.swing.table.DefaultTableModel();
        Vector<Vector<Object>> dataAuteurs = new Vector<Vector<Object>>();
        dataAuteurs.clear();
        Vector<String> columnAuteurs = new Vector<String>();
        columnAuteurs.clear();
        columnAuteurs.add("Nom");
        columnAuteurs.add("Prenom");
        for(Auteur aut:auteurs) {
            Vector<Object> data_lineAuteurs = new Vector<Object>();
             data_lineAuteurs.add(aut.nom());
             data_lineAuteurs.add(aut.prenom());
             dataAuteurs.add(data_lineAuteurs);
        }
       dtmAuteurs.setDataVector(dataAuteurs, columnAuteurs);
       auteurs_table.setModel(dtmAuteurs);
    }

     private void afficherListeMotsCles() {
        Set<MotCle> motsCles  = ouvrage.tesMotsCles();
        javax.swing.table.DefaultTableModel dtmMotsCles = new javax.swing.table.DefaultTableModel();
        Vector<Vector<Object>> dataMotsCles = new Vector<Vector<Object>>();
        dataMotsCles.clear();
        Vector<String> columnMotsCles = new Vector<String>();
        columnMotsCles.clear();
        columnMotsCles.add("Liste mot clé");
        for(MotCle mc:motsCles) {
            Vector<Object> data_lineMotsCles = new Vector<Object>();
             data_lineMotsCles.add(mc.motCle());
             dataMotsCles.add(data_lineMotsCles);
        }
       dtmMotsCles.setDataVector(dataMotsCles, columnMotsCles);
       motscles_table.setModel(dtmMotsCles);
    }

    private void afficherListeExemplaires() {
      Enumeration enumEx = ouvrage.enumExemplaires();

      javax.swing.table.DefaultTableModel dtmExemp = new javax.swing.table.DefaultTableModel();
      Vector<Vector<Object>> dataExemp = new Vector<Vector<Object>>();
      dataExemp.clear();
      Vector<String> columnExemp = new Vector<String>();
      columnExemp.clear();
      columnExemp.add("Numéro");
      columnExemp.add("Statut");
      if  (enumEx.hasMoreElements()) {
          while (enumEx.hasMoreElements()) {
              Vector<Object> data_lineExemp = new Vector<Object>();
              Exemplaire exemp = (Exemplaire) enumEx.nextElement();
              data_lineExemp.add(Integer.toString(exemp.numero()));
              data_lineExemp.add(exemp.libStatut());
              dataExemp.add(data_lineExemp);
          }
      }
      
      dtmExemp.setDataVector(dataExemp, columnExemp);
      exemplaires_table.setModel(dtmExemp);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        terminer_button = new javax.swing.JButton();
        auteur_label = new javax.swing.JLabel();
        titre_label = new javax.swing.JLabel();
        editeur_label = new javax.swing.JLabel();
        date_label = new javax.swing.JLabel();
        titre_textfield = new javax.swing.JTextField();
        editeur_textfield = new javax.swing.JTextField();
        date_textfield = new javax.swing.JTextField();
        isbn_label = new javax.swing.JLabel();
        isbn_textfield = new javax.swing.JTextField();
        nbexemp_label = new javax.swing.JLabel();
        nbexemp_textfield = new javax.swing.JTextField();
        exemplaires_label = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        exemplaires_table = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        auteurs_table = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        motscles_table = new javax.swing.JTable();
        auteur_label1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Vue d'un ouvrage");
        setResizable(false);

        terminer_button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/biblio/flag_red.png"))); // NOI18N
        terminer_button.setText("Terminer");
        terminer_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                terminer_buttonMousePressed(evt);
            }
        });

        auteur_label.setText("Auteur(s) :");

        titre_label.setText("Titre :");

        editeur_label.setText("Editeur :");

        date_label.setText("Date edition :");

        titre_textfield.setEditable(false);

        editeur_textfield.setEditable(false);

        date_textfield.setEditable(false);
        date_textfield.setHorizontalAlignment(javax.swing.JTextField.LEFT);

        isbn_label.setText("Numero ISBN :");

        isbn_textfield.setEditable(false);

        nbexemp_label.setText("Nombre d'exemplaires :");

        nbexemp_textfield.setEditable(false);
        nbexemp_textfield.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        exemplaires_label.setText("Exemplaires :");

        exemplaires_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numero", "Statut"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        exemplaires_table.setCellSelectionEnabled(true);
        exemplaires_table.setEnabled(false);
        exemplaires_table.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(exemplaires_table);
        exemplaires_table.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        exemplaires_table.getColumnModel().getColumn(0).setResizable(false);
        exemplaires_table.getColumnModel().getColumn(1).setResizable(false);

        auteurs_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "nom", "prenom"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        auteurs_table.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(auteurs_table);
        auteurs_table.getColumnModel().getColumn(0).setResizable(false);
        auteurs_table.getColumnModel().getColumn(1).setResizable(false);

        motscles_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "liste mot clé"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(motscles_table);
        motscles_table.getColumnModel().getColumn(0).setResizable(false);

        auteur_label1.setText("Mot(s) clé(s) :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(isbn_label, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(titre_label, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(titre_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                            .addComponent(isbn_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE))
                        .addGap(23, 23, 23))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(auteur_label, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(editeur_label, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addComponent(editeur_textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                        .addGap(21, 21, 21))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(date_label, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(exemplaires_label, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nbexemp_label))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                            .addComponent(date_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nbexemp_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(terminer_button, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addContainerGap(21, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(auteur_label1, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {auteur_label, auteur_label1, date_label, editeur_label, exemplaires_label, isbn_label, nbexemp_label, titre_label});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {date_textfield, editeur_textfield, isbn_textfield, jScrollPane1, jScrollPane2, jScrollPane3, titre_textfield});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(isbn_label)
                    .addComponent(isbn_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(titre_label)
                    .addComponent(titre_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(auteur_label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editeur_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editeur_label))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(auteur_label1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(date_label, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(date_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nbexemp_label)
                    .addComponent(nbexemp_textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exemplaires_label))
                .addGap(18, 18, 18)
                .addComponent(terminer_button)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void terminer_buttonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_terminer_buttonMousePressed
        // TODO add your handling code here:
        // Vérification la non existence de l'ouvrage / ISBN
        this.dispose();
}//GEN-LAST:event_terminer_buttonMousePressed


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VueOuvrageGUI dialog = new VueOuvrageGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel auteur_label;
    private javax.swing.JLabel auteur_label1;
    private javax.swing.JTable auteurs_table;
    private javax.swing.JLabel date_label;
    private javax.swing.JTextField date_textfield;
    private javax.swing.JLabel editeur_label;
    private javax.swing.JTextField editeur_textfield;
    private javax.swing.JLabel exemplaires_label;
    private javax.swing.JTable exemplaires_table;
    private javax.swing.JLabel isbn_label;
    private javax.swing.JTextField isbn_textfield;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable motscles_table;
    private javax.swing.JLabel nbexemp_label;
    private javax.swing.JTextField nbexemp_textfield;
    private javax.swing.JButton terminer_button;
    private javax.swing.JLabel titre_label;
    private javax.swing.JTextField titre_textfield;
    // End of variables declaration//GEN-END:variables

     public void update(Observable obs, Object o) {
       // maj de la vue
       this.afficherTout();
     }  // Fin upDate

}
